package com.sduept.nwld.dataserver.service.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.enums.ItemTypeEnum;
import com.sduept.bigdata.equipment.assessment.exception.ParameterException;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionItem;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionPlan;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionStation;
import com.sduept.bigdata.equipment.assessment.service.InspectionItemService;
import com.sduept.bigdata.equipment.assessment.service.InspectionStationService;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.entity.RoleEntityBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 评价进度Service，用于获取在指定的计划中，当前用户可见的变电站
 * @author huangyu
 * @Date 2019年11月14日
 */
@Slf4j
@Service
public class InspectionPlanProgressService {

	@Autowired
	private InspectionStationService stationService;
	@Autowired
	private InspectionItemService itemService;
	
	/**
	 * 根据人员和计划id,返回该人员所能看到的InspectionStation以及List<InspectionItem>
	 * @param plan
	 * @param user
	 * @return
	 * @throws ParameterException
	 */
	public Map<InspectionStation, List<InspectionItem>> findInspectionStations(InspectionPlan plan,PersonnelEntityBean user) throws ParameterException {
		if(null == plan || null == user) {
			return null;
		}
		List<InspectionStation> stations = findInspectionStationsByPlanAndUser(plan, user);
		return genStationsAndItemsMap(stations, plan.getId());
	}
	/**
	 * 在评价进度下，当前人员所能看到的指定计划下的变电站
	 * @param plan
	 * @param user
	 * @return
	 * @throws ParameterException
	 */
	// TODO 该方法应该放在InspectionStationService类中
	private List<InspectionStation> findInspectionStationsByPlanAndUser(InspectionPlan plan,PersonnelEntityBean user) throws ParameterException {
		List<InspectionStation> stations = new ArrayList<InspectionStation>();
		if(null == plan) {
			return null;
		}
		String planId = plan.getId();
		// 专家 || 总调人员
		if(isAdmin(user) || isZongDiao(user)) {
			Set<InspectionStation> stationSets = stationService.findByInspectionPlan(plan.getId());
			stations.addAll(stationSets);
		} else {
			// 被检单位迎检联系人
			List<InspectionStation> list1 = stationService.findByPlanIdAndStationPersonnel(planId, user.getName());
			// 派出专家分子公司联系人
			List<InspectionStation> list2 = stationService.findByPlanIdAndInspectionScopePersonnel(plan, user.getName());
//			List<InspectionStation> list2 = stationService.findByPlanIdAndInspectionScopePersonnel(planId, user.getName());
			// 派出专家
			List<InspectionStation> list3 = stationService.findByPlanIdAndExpertCode(planId, user.getCode());
			stations.addAll(list1);
			stations.addAll(list2);
			stations.addAll(list3);
		}
		return stations;
	}

	private Map<InspectionStation, List<InspectionItem>> genStationsAndItemsMap(List<InspectionStation> stations,String planId) {
		Map<InspectionStation,List<InspectionItem>> stationAndItemMap = new HashMap<InspectionStation, List<InspectionItem>>();
		for (InspectionStation sta : stations) {
			try {
				List<InspectionItem> items = itemService.findByPlanIdAndInspectionStationIdAndType(planId, sta.getId(), ItemTypeEnum.INSPECTIONCRITERION);
				if(null != items && items.size()>0) {
					stationAndItemMap.put(sta, items);
				}
			} catch (ParameterException e) {
				e.printStackTrace();
				log.info("获取检查站的评价项目出错");
			}
		}
		return stationAndItemMap;
	}
	/**
	 * 判断该用户是否为专家
	 * @param user
	 * @return
	 */
	private boolean isAdmin(PersonnelEntityBean user) {
		Set<RoleEntityBean> roles = user.getRoles();
		for (RoleEntityBean role : roles) {
			if("管理员".equals(role.getName())) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 判断该用户是否为总调人员
	 * @param user
	 * @return
	 */
	private boolean isZongDiao(PersonnelEntityBean user) {
		DepartmentEntityBean dept = user.getGroup();
		if(dept.getName().contains("总调")) {
			return true;
		}
		return false;
	}
}
